﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Megaman
{
    class Bullet
    {

        public Vector2 Position;

 
        public Animation BulletAnimation;

        public AnimationPlayback<States> Playback;

        public enum States
        {
            starting,
            desintegrating
        }
        public enum Heading{
            Left,
            Right,
        }
        public Heading heading;
        public float Velocity;
        public int energy;
        private Rectangle localBounds;
        public Rectangle BoundingRectangle
        {
            get
            {
                int left = (int)Math.Round(Position.X - Playback.Origin().X) + localBounds.X;
                int top = (int)Math.Round(Position.Y - Playback.Origin().Y) + localBounds.Y;

                return new Rectangle(left, top, localBounds.Width, localBounds.Height);
            }
        }

      
        private bool bulletAway = false;
        private const float BARREL_LENGTH = 0.125f;
        private float barrel_elapsed = 0f;
        private  float BULLET_LIFE_SPAN = 2f;
        private float elapsed_bullet_life_time = 0f;

        public bool desintegrated = false;

        public Bullet( Animation bullet, Vector2 origin, Heading heading, float velocity, int damage)
        {
            
            BulletAnimation = bullet;
            Position = origin;
            this.heading = heading;
 
            Velocity = velocity;
            Playback.PlayAnimation(BulletAnimation,States.starting);
            this.energy = damage;
            
        }

        public void Update(GameTime gameTime)
        {
            elapsed_bullet_life_time += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (elapsed_bullet_life_time > BULLET_LIFE_SPAN)
            {
                desintegrated = true;
            }

            barrel_elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (barrel_elapsed > BARREL_LENGTH)
            {
                bulletAway = true;
            }
        

            if (bulletAway)
            {
                int displacement = (int)(Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);
                Playback.PlayAnimation(BulletAnimation,States.starting);
                localBounds = Playback.LocalBounds();
                if (heading == Heading.Left)
                {
                    Position.X -= displacement;
                }
                else
                    Position.X += displacement;
                
            }
        }

        public void Draw(SpriteBatch spritebatch, GameTime gameTime)
        {
                BULLET_LIFE_SPAN = spritebatch.GraphicsDevice.Viewport.Width / Velocity;
                SpriteEffects effects;
                if (heading == Heading.Left)
                {
                    effects = SpriteEffects.FlipHorizontally;
                }
                else
                    effects = SpriteEffects.None;
            if(bulletAway)
                Playback.Draw(gameTime, spritebatch, Position, effects);
            
        }
    }
}
